//Base class for a controller

    //A controller needs to handle the following
        //- listing fetching displaying
        //- add delete

var baseControl = new Class({

	initialize: function ()
		{
			this.count = 0;
			this.items = {};		
			this.stringType = 'baseController';
			this.itemType = 'none';
		},
        
/* Handler, for handling the requests for the item model */

		baseHandler: function(request, data, callback)
			{
				switch(request)
				{
					case 'add': {
						return this.add(data, callback);
					} break;
						
					case 'fetch': {
						return this.fetch(data, callback);
					} break;
					
				}
			},
		
/*  Add an item
		object : the iten object to add, usually from the child class type
		callback : if succeeded this gets called.
		returns: the ID of the new item or -1 if it fails */
        
		baseAdd: function(data, callback)
			{	
                //-1 if trying to add the controller itself.
				if(this.itemType == 'none') return -1;
									
				//We do allow json string based object as well as an actual entity
				if($type(data) == 'string')
				{
					data = up(data);
					data = eval("new " + this.itemType + '(data);'); //new Target(data);
				}
				
				this.count += 1;
				index = this.count - 1;
				
				this.items[index] = data;
                this.items[index].id = this.count;
				
				if( $defined(callback) && callback != '' )
					callback.call('', true);
					
				return this.count;
			},
            
/*  Delete an item
        items : the list of items to delete, space separated string. 
        callback : a callback to call when complete */
        
        baseDelete: function(items, callback)
            {
                //When we delete, we 'send it to the trash' instead.
                //This keeps it in the database, out of the lists.
				if($type(items) == 'string')
				{
					var aList = items.split(' ');
					var count = aList.length;
					for(i = 0; i < count; ++i)
					{
						var index = aList[i];
						if( (index > 0) && (index <= this.count) )
						{
                            //Change its status to trashed
							this.items[ index - 1 ].edit('internalStatus','inactive');
						}
					}
					
					if( $defined(callback)  && callback != '' )
						callback.call('', true);
						
					return true;
				}
				
				return false;
            },
            
/*
	Fetch an item or group of items by id's
		items: a space seperated list of id's
		callback: the function to call when done (passing in the array)
*/
		fetch: function(items, callback)
			{
				var retArr = new Array();
				if($type(items) == 'string')
				{
					var aList = items.split(' ');
					var count = aList.length;
					for(i = 0; i < count; ++i)
					{
						var index = aList[i];
						if( (index > 0) && (index <= this.count) )
						{
                            //if(this.items[ index - 1 ].inactiveStatus != 'inactive')
                                retArr[i] = this.items[ index - 1 ];
						}
					}
					
					if( $defined(callback)  && callback != '' )
						callback.call('', retArr);
					else
						return retArr;
						
					return true;
				}
				
				return false;
			},             
            
            
		
/*	List all items in the manager using debug printout. */
		toStrAll: function()
			{
				var str = '';
				for(i = 0; i < this.count; ++i)
				{
					str += this.items[i].toStr();
				}
				
				return str;
			},
	
/* List items by ID
		items: space seperated list of id's to list */

		toStrList: function(items, callback)
			{
				var str = '';
				
				if(items)
				{
					var aList = items.split(' ');
					var count = aList.length;
					
					for(i = 0; i < count; ++i)
					{
						var index = aList[i];
						if( (index > 0) && (index <= this.count) )
						{
							str += this.items[ index - 1 ].toStr();
						}
					}
				}
				else
				{
					str = this.toStrAll();
				}
				
				return str;
			},
	
/*
	Update the entries in this.items with the database tables.
*/
		update: function()
			{
				//Create an async call to the php/mysql to update the projects properties
			},
	
/*
	Commit all client/local changes to the database.
	note://i would like to add some kind of dirty flag, only updating the required changes
*/
		commit: function()
			{
				
			}
	
});